package com.vova.rec.grpc.client;

import com.vova.rec.grpc.common.BooleanReply;
import com.vova.rec.grpc.common.ConditionsRequest;
import com.vova.rec.grpc.common.DeviceFix;
import com.vova.rec.grpc.common.DeviceFixServiceGrpc;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController("/test")
public class TestController {

    @GrpcClient("server")
    private DeviceFixServiceGrpc.DeviceFixServiceBlockingStub deviceFixServiceBlockingStub;

    @PostMapping
    public Boolean insertDeviceFix(@RequestBody DeviceFixRequest request) {
        BooleanReply booleanReply = deviceFixServiceBlockingStub.insertDeviceFix(DeviceFix.newBuilder()
                .setType(request.getType()).setId(request.getId()).setAddress(request.getAddress()).build());
        log.info("insert param={}, res={}", request, booleanReply);
        return booleanReply.getReply();
    }

    @GetMapping
    public Boolean searchDeviceFix(@RequestParam String id) {
        DeviceFix deviceFix = deviceFixServiceBlockingStub.searchDeviceFix(ConditionsRequest.newBuilder().setId(id).build());
        log.info("search id={},res={}", id, deviceFix);
        return deviceFix != null;
    }

    @GetMapping("/all")
    public Boolean getAll() {
        return false;
    }

    @DeleteMapping
    public Boolean deleteDeviceFix(@RequestParam String id) {
        BooleanReply booleanReply = deviceFixServiceBlockingStub.deleteDeviceFix(ConditionsRequest.newBuilder().setId(id).build());
        log.info("delete id={},res={}", id, booleanReply.getReply());
        return booleanReply.getReply();
    }

    @Data
    public static class DeviceFixRequest {
        private String id;
        //        private String serialNum;
//        private String userNum;
//        private Integer status;
        private Integer type;
        private String address;
//        private String createtime;
//        private String updatetime;
    }
}
